package JZOffer;

import 树.easy.ErSanWu;

/**
 * @author Lw中
 * @date 2021/7/26 22:16
 */
public class Jz68 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    // Jz68 I.二叉搜索树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode res = root;
        while (true) {
            if (p.val < res.val && q.val < res.val) {
                res = res.left;
            }
            else if(p.val > res.val && q.val > res.val) {
                res = res.right;
            }
            else break;
        }
        return res;
    }


}
